import { api } from '@/axios/instance';
import { toast } from 'sonner';

export type GetColumnsByProject = AxiosRequestConfig<{ project_id: string }>;

export const getColumnsByProject = async ({ params, config }: GetColumnsByProject) =>
  api
    .post<PostColumnResponse>('/api/TaskBoard/projectColumns', params, config)
    .then((response) => response.data.data);

export type PostColumnParams = Omit<Column, 'id' | 'tasks'>;
type PostColumnParamsWithProjectID = {
  project_id: string;
} & PostColumnParams;
export type PostColumnConfig = AxiosRequestConfig<PostColumnParamsWithProjectID>;

export const postColumn = async ({ params, config }: PostColumnConfig) =>
  api.post<PostColumnResponse>('/api/TaskBoard/columnCreate', params, config).then((response) => {
    if (response.data.success === 'ok') {
      toast.success('Column Created');
      return response.data.data;
    }
  });

export type DeleteColumnConfig = AxiosRequestConfig<Pick<Column, 'id'> & { project_id: string }>;

export const axiosDeleteColumn = async ({ params, config }: DeleteColumnConfig) =>
  api.post('/api/TaskBoard/deleteColumn', params, config).then((response) => {
    if (response.data.success === 'ok') {
      toast.success('Column deleted');
    }
  });

export type UpdateColumnParams = AxiosRequestConfig<{
  id: number;
  name: string;
  project_id: string;
}>;
export const axiosRenameColumn = async ({ params, config }: UpdateColumnParams) =>
  api.post('/api/TaskBoard/columnRename', params, config);
